<?php
namespace app\question\controller\admin;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use app\question\model\QuestionCategory;
use think\console\command\make\Controller;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="题目分类",
 *     parent="question/admin.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Questcate extends Admin
{
    use ControllerCurd;
    public $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new QuestionCategory();
    }

    /**
     * @NodeAnnotation(
     *     title="题目分类",
     *     parent="question/admin.questcate",
     *     MenuRoot="question/admin.index"
     * )
     */
    public function index()
    {
        $this->assign([
            "cateList" => $this->model->selectPageDatas()
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加题目分类",
     *     parent="question/admin.questcate",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function addcategory(Request $request)
    {
        if($request->isAjax())
        {
            $tree = $request->param("dataType");
            if($tree=='tree')
            {
                $data = $this->model->select();
                return json($data);
            }
        }
        $this->add(url('question/admin.questcate/index'));
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑题目分类",
     *     parent="question/admin.questcate",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function editcategory(Request $request,$id=0)
    {
        $category = $this->model->findData($id);
        $this->assign([
            "category" => $category,
            "level"    => $this->model->find($category['pid'])
        ]);
        $this->edit(url('question/admin.questcate/index'));
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除题目分类",
     *     parent="question/admin.questcate",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function delcategory()
    {
        $this->del();
    }
}